package org.qs.webservice.service.impl;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.qs.webservice.content.IReadContent;
import org.qs.webservice.dao.content.ConBookStatMapper;
import org.qs.webservice.dao.contentWrite.WriteConBookStatMapper;
import org.qs.webservice.dao.user.UsBookReviewMapper;
import org.qs.webservice.dao.user.UsBookShelfMapper;
import org.qs.webservice.dao.user.UsConsumeMapper;
import org.qs.webservice.dao.user.UsMemberPointMapper;
import org.qs.webservice.domain.ConBookStat;
import org.qs.webservice.domain.UsMemberPoint;
import org.qs.webservice.exception.QSReadException;
import org.qs.webservice.service.UpdateDataService;
import org.springframework.stereotype.Service;

@Service
public class UpateDataServiceImpl implements UpdateDataService {
	@Resource
	private UsBookReviewMapper usBookReviewMapper;
	@Resource
	private WriteConBookStatMapper writeConBookStatMapper;
	@Resource
	private ConBookStatMapper conBookStatMapper;
	@Resource
	private UsConsumeMapper usConsumeMapper;
	@Resource
	private UsBookShelfMapper usBookShelfMapper;
	@Resource
	private UsMemberPointMapper usMemberPointMapper;
	public boolean updateConBookStat(ConBookStat conBookStat,int book_id,int type,int num,ConBookStat new_conBookStat,int fee){
		if(new_conBookStat==null){
			new_conBookStat= new ConBookStat();
		}
		int ret  = 0;
		int temp=0;
		if(conBookStat==null){
			temp=1;//插入数据
			conBookStat = new ConBookStat();
			conBookStat.setBook_id(book_id);
			if(type==1){//setFavorites
				conBookStat.setFavorites(usBookShelfMapper.getShelfNumByBook(book_id));
				conBookStat.setOrders(0);
				conBookStat.setReviews(0);
				conBookStat.setRewards(0);
				conBookStat.setVotes(0);
				conBookStat.setPv(0);
			}else if(type==2){//setVotes
				conBookStat.setFavorites(0);
				conBookStat.setOrders(0);
				conBookStat.setReviews(0);
				conBookStat.setRewards(0);
				conBookStat.setVotes(usConsumeMapper.getUsSumFeeCountByBookId(book_id, 4));
			}else if(type==3){//setRewards
				conBookStat.setFavorites(0);
				conBookStat.setOrders(0);
				conBookStat.setReviews(0);
				conBookStat.setRewards(usConsumeMapper.getUsSumFeeCountByBookId(book_id, 3));
				conBookStat.setNum(num);
				conBookStat.setVotes(0);
			}else if(type==4){//setReviews
				conBookStat.setFavorites(0);
				conBookStat.setOrders(0);
				conBookStat.setReviews(usBookReviewMapper.getBookReviewCountByList(book_id, 0, 0,0));
				conBookStat.setRewards(0);
				conBookStat.setVotes(0);
			}else  if(type==5){//setOrders
				conBookStat.setFavorites(0);
				conBookStat.setOrders(usConsumeMapper.getUsConsumeListCountByParam(null, book_id, 1, null, null));
				conBookStat.setOrder_fee(usConsumeMapper.getUsConsumeSumByParam(null, book_id, 1, null, null));
				conBookStat.setReviews(0);
				conBookStat.setRewards(0);
				conBookStat.setVotes(0);
			}else  if(type==6){//setPv
				conBookStat.setFavorites(0);
				conBookStat.setOrders(0);
				conBookStat.setReviews(0);
				conBookStat.setRewards(0);
				conBookStat.setVotes(0);
				conBookStat.setPv(1);
			}
			try {
				ret = writeConBookStatMapper.insertConBookStat(conBookStat);
			} catch (Exception e) {
				ret = 0;
			}
		}
		if(ret ==1){
			new_conBookStat.setBook_id(book_id);
			new_conBookStat.setFavorites(conBookStat.getFavorites());
			new_conBookStat.setOrders(conBookStat.getOrdesr());
			new_conBookStat.setPv(conBookStat.getPv());
			new_conBookStat.setReviews(conBookStat.getReviews());
			new_conBookStat.setRewards(conBookStat.getRewards());
			new_conBookStat.setUser_id(conBookStat.getUser_id());
			new_conBookStat.setVotes(conBookStat.getVotes());
			new_conBookStat.setOrder_fee(conBookStat.getOrder_fee());
			return true;
		}else if(temp==1){
			conBookStat = conBookStatMapper.getBookStatById(book_id);
		}
		for(int i =0 ;i<10;i++){
			ConBookStat conBookStatUpdate = new ConBookStat(); 
			conBookStatUpdate.setBook_id(book_id);
			conBookStatUpdate.setFavorites(-1);
			conBookStatUpdate.setOrders(-1);
			conBookStatUpdate.setReviews(-1);
			conBookStatUpdate.setRewards(-1);
			conBookStatUpdate.setVotes(-1);
			conBookStatUpdate.setPv(-1);
			conBookStatUpdate.setOrder_fee(0);
			if(type==1){
				conBookStatUpdate.setFavorites(conBookStat.getFavorites());
			}else if(type==2){
				conBookStatUpdate.setVotes(conBookStat.getVotes());
			}else if(type==3){
				conBookStatUpdate.setRewards(conBookStat.getRewards());
				conBookStatUpdate.setNum(num);
			}else if(type==4){
				conBookStatUpdate.setReviews(conBookStat.getReviews());
				conBookStatUpdate.setNum(num);
			}else if(type==5){
				conBookStatUpdate.setOrders(conBookStat.getOrdesr());
				conBookStatUpdate.setOrder_fee(fee);
			}else if(type==6){
				conBookStatUpdate.setPv(conBookStat.getPv());
			}
			ret = writeConBookStatMapper.updateConBookStatNum(conBookStatUpdate);
			//ret = writeConBookStatMapper.updateConBookStatNum(conBookStat);
			if(ret ==1){
				new_conBookStat.setBook_id(book_id);
				new_conBookStat.setFavorites(conBookStat.getFavorites());
				new_conBookStat.setOrders(conBookStat.getOrdesr());
				new_conBookStat.setPv(conBookStat.getPv());
				new_conBookStat.setReviews(conBookStat.getReviews());
				new_conBookStat.setRewards(conBookStat.getRewards());
				new_conBookStat.setUser_id(conBookStat.getUser_id());
				new_conBookStat.setVotes(conBookStat.getVotes());
				return true;
			}else{
				conBookStat = conBookStatMapper.getBookStatById(book_id);
			}
		}
		
		return false;
		
		
	}
	@Override
	public void updateMemberPoint(UsMemberPoint usMemberPoint,int num,int point,Long user_id) {
		try {
			int ret=0;
			try {
				if(usMemberPoint!=null)
					ret = usMemberPointMapper.updatePoint(usMemberPoint);
			} catch (Exception e1) {
				ret=0;
			}
			try {
				if(ret !=1){
					for(int i =0;i<10;i++){
						UsMemberPoint base = usMemberPointMapper.getPoint(user_id);
						if(base==null){
							base = new UsMemberPoint();
							base.setUser_id(user_id);
							base.setPoint(IReadContent.base_point);
							base.setVersion(0);
							try {
								usMemberPointMapper.addPoint(base);
							} catch (Exception e) {
								continue;
							}
						}
						int consume_fee =base.getConsume_fee()+num;
						base.setConsume_fee(consume_fee%50);
						base.setPoint(base.getPoint()+consume_fee/50+point);
						ret = usMemberPointMapper.updatePoint(base);
						if(ret==1){
							break;
						}
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		} catch (Exception e) {
			Map<String,Object> errorJson = new HashMap<String, Object>();
			errorJson.put("user_id", usMemberPoint.getUser_id());
			QSReadException.printf(e, "增加积分异常");
		}
	}
	
}
